package kmi.droid.animatron;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;

public class ParticleSystem<T extends Particle> extends ParticleSystemBase
{
	protected List<T> particles = new ArrayList<T>();

	@Override
	public boolean getIsAlive()
	{
		return particles.size() > 0;
	}

	public ParticleSystem()
	{
	}

	@Override
	public void update(float dt)
	{
		for (int i = 0; i < particles.size(); i++)
		{
			particles.get(i).update(dt);
		}

		removeDeadParticles();
	}

	private void removeDeadParticles()
	{
		List<T> newDeads = new ArrayList<T>();

		for (int i = 0; i < particles.size(); i++)
		{
			if (!particles.get(i).getIsAlive())
				newDeads.add(particles.get(i));
		}

		for (int i = 0; i < newDeads.size(); i++)
			particles.remove(newDeads.get(i));
	}

	@Override
	public void draw(Canvas canvas)
	{
		for (int i = 0; i < particles.size(); i++)
		{
			particles.get(i).draw(canvas);
		}
	}
}
